package com.rdc.example.dao.impl;

import com.rdc.example.dao.SongLikesDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.rdc.example.util.DBUtil.closeResources;
import static com.rdc.example.util.DBUtil.getConnection;

public class SongLikesDaoImpl implements SongLikesDao {

    @Override
    public boolean insertSongLike(Integer userId, Integer songId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //获取连接
            conn = getConnection();
            String sql = "insert into `song_likes` (user_id, song_id) values (?, ?)";
            pstmt = conn.prepareStatement(sql);
            //设值
            pstmt.setInt(1, userId);
            pstmt.setInt(2, songId);
            //按照影响行数row返回结果
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLException e) {
            if (e.getErrorCode() == 1062) {
                System.out.println("重复点赞: user=" + userId + ", songId=" + songId);
            } else {
                throw e;
            }
        } finally {
            //释放资源
            closeResources(null, pstmt, conn);
        }
        return false;
    }

    @Override
    public boolean deleteSongLike(Integer userId, Integer songId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //获取连接
            conn = getConnection();
            String sql = "delete from `song_likes` where user_id=? and song_id=? ";
            pstmt = conn.prepareStatement(sql);
            //设值
            pstmt.setInt(1, userId);
            pstmt.setInt(2, songId);
            //按照影响行数row返回结果
            int row = pstmt.executeUpdate();
            return row > 0;
        } finally {
            //释放资源
            closeResources(null, pstmt, conn);
        }
    }

    @Override
    public boolean checkSongLike(Integer userId, Integer songId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            // 获取连接
            conn = getConnection();
            // 定义查询语句 查询有没有时select 1 比count 更快
            String sql = "select 1 from `song_likes` where user_id = ? and song_id = ?";
            pstmt = conn.prepareStatement(sql);
            // 设置参数
            pstmt.setInt(1, userId);
            pstmt.setInt(2, songId);
            // 执行查询操作
            rs = pstmt.executeQuery();
            // 如果结果集有下一行，说明存在记录，即用户已点赞
            return rs.next();
        } finally {
            // 释放资源
            closeResources(rs, pstmt, conn);
        }
    }

    @Override
    public List<Integer> getSongIdListByUserId(Integer userId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Integer> songIds = new ArrayList<>();
        try {
            conn = getConnection();
            // 直接查询song_id字段
            String sql = "select song_id from `song_likes` where user_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            rs = pstmt.executeQuery();
            // 遍历结果集提取
            while (rs.next()) {
                songIds.add(rs.getInt("song_id"));
            }
        } finally {
            // 释放资源
            closeResources(rs, pstmt, conn);
        }

        return songIds;
    }







}
